package com.word.teacher.service.impl;

import java.util.List;
import com.word.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.word.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.word.teacher.domain.WordGroup;
import com.word.teacher.mapper.WordMapper;
import com.word.teacher.domain.Word;
import com.word.teacher.service.IWordService;
import com.word.common.core.text.Convert;

/**
 * 单词信息管理Service业务层处理
 * 
 * @author wsccvb
 * @date 2025-05-20
 */
@Service
public class WordServiceImpl implements IWordService 
{
    @Autowired
    private WordMapper wordMapper;

    /**
     * 查询单词信息管理
     * 
     * @param id 单词信息管理主键
     * @return 单词信息管理
     */
    @Override
    public Word selectWordById(Long id)
    {
        return wordMapper.selectWordById(id);
    }

    /**
     * 查询单词信息管理列表
     * 
     * @param word 单词信息管理
     * @return 单词信息管理
     */
    @Override
    public List<Word> selectWordList(Word word)
    {
        if (word.getWordGroupIdList() != null && !word.getWordGroupIdList().isEmpty()) {
            List<WordGroup> wordGroupList = wordMapper.selectWordGroupListByGroupIdList(word.getWordGroupIdList());
            return wordMapper.selectWordListByIdList(wordGroupList.stream()
                    .map(WordGroup::getWordId)
                    .distinct()
                    .collect(Collectors.toList()));
        }
        return wordMapper.selectWordList(word);
    }

    /**
     * 新增单词信息管理
     * 
     * @param word 单词信息管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWord(Word word)
    {
        word.setCreateTime(DateUtils.getNowDate());
        int rows = wordMapper.insertWord(word);
        insertWordGroup(word);
        return rows;
    }

    /**
     * 修改单词信息管理
     * 
     * @param word 单词信息管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWord(Word word)
    {
        word.setUpdateTime(DateUtils.getNowDate());
        wordMapper.deleteWordGroupByWordId(word.getId());
        insertWordGroup(word);
        return wordMapper.updateWord(word);
    }

    /**
     * 批量删除单词信息管理
     * 
     * @param ids 需要删除的单词信息管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWordByIds(String ids)
    {
        wordMapper.deleteWordGroupByWordIds(Convert.toStrArray(ids));
        return wordMapper.deleteWordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除单词信息管理信息
     * 
     * @param id 单词信息管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWordById(Long id)
    {
        wordMapper.deleteWordGroupByWordId(id);
        return wordMapper.deleteWordById(id);
    }

    /**
     * 新增单词分组信息
     * 
     * @param word 单词信息管理对象
     */
    public void insertWordGroup(Word word)
    {
        List<Long> wordGroupList = word.getWordGroupIdList();
        Long id = word.getId();
        if (StringUtils.isNotNull(wordGroupList))
        {
            List<WordGroup> list = new ArrayList<WordGroup>();
            for (Long wordGroupId : wordGroupList)
            {
                WordGroup wordGroup = new WordGroup();
                wordGroup.setWordId(id);
                wordGroup.setGroupId(wordGroupId);
                list.add(wordGroup);
            }
            if (list.size() > 0)
            {
                wordMapper.batchWordGroup(list);
            }
        }
    }
}
